<?php
/**
 * 调查问卷
 */
class IndexControl extends Control
{  
	private $model;
	private $s_time='20140501';
	private $survey_finish_date = '20140514';
	private $uin='';
	private $zoneid='';
	
    /**
     * 构造函数
     */
	function __construct(){
		parent::__construct();
		load_config(ACT_FILE.'/onload');
		load_model('main');
		$this->model = new MainModel();
		//重新设置活动时间（用于外网、预发布环境）
		if(file_exists(APPLICATION_ROOT.'/config/basic.cfg.php')){
			$BasicCFG=include_once APPLICATION_ROOT.'/config/basic.cfg.php'; 
			$actName=!empty($BasicCFG['2014']['dcwj'])?$BasicCFG['2014']['dcwj']:'';
			if ( !empty($actName) ){
				$this->s_time=$actName['s_time'];
				$this->survey_finish_date=$actName['e_time'];
			}
		}
		
	}
	
	function index(){
		$view = array();
		$view['survey_finish_date'] = $this->survey_finish_date;
		$view['s_time']=date('Y年m月d日',strtotime($this->s_time));
		$view['e_time']=date('Y年m月d日',strtotime($this->survey_finish_date));
		
		load_view(ACT_FILE.'/index',$view);
	}
	
	function submit(){
		if(date('Ymd') > $this->survey_finish_date){
			dumpErrorCode('该调查问卷已于'.$this->survey_finish_date.'结束了，非常感谢您的参与！', DOMAIN_URL);
		}
		
		if($this->isLogin()){
			dumpErrorCode('提交失败，请重新登录后再试！', DOMAIN_URL);
		}
		
		$rows=$this->model->getRows('survey',"where uin={$this->uin} AND zone_id={$this->zoneid}");
		if($rows){			
			dumpErrorCode('您在'.$_SESSION['zoneName'].'已经回答过调查问卷了，非常感谢您的参与！', DOMAIN_URL);
		}
		
		for($i=1;$i<=11;$i++){
			$qname = 'q'.$i;
			if(empty($_POST[$qname])){
				dumpErrorCode('请选择第'.$i.'题的答案，谢谢！', DOMAIN_URL);
			}
		}

		$record = array();
		$record['uin']					= $_SESSION['UIN'];
		$record['zone_id']			= $this->zoneid;
		$record['account']			= $_SESSION['LoginUser'];
		$record['qq']					= 0;
		$record['submit_ip']		= get_client_ip_from_ns(true);
		$record['submit_time']	= date('Y-m-d H:i:s');
		$record['answer_1']=$_POST['q1'];
		$record['answer_2']=$_POST['q2'];
		$record['answer_3']=$_POST['q3'];
		$record['answer_4']=$_POST['q4'];
		$record['answer_5']=$_POST['q5'];
		$record['answer_6']=$_POST['q6'];
		$record['answer_7']=$_POST['q7'];
		$record['answer_8']=$_POST['q8'];
		$record['answer_9']=join(',', $_POST['q9']);
		$record['answer_10']=$_POST['q10'];
		$record['answer_11']=$_POST['q11'];
		$record['answer_12']='0';
		$record = Text::formSafe($record, get_magic_quotes_gpc());
		$record = Text::safeSql($record);
		$ret=$this->model->insert_data('survey', $record);
		
		if($ret){
			$_SESSION['HasAnswer'] = true;
			dumpErrorCode('提交成功，非常感谢您的参与！', DOMAIN_URL);
		}else{
			dumpErrorCode('提交失败，请稍后再试！', DOMAIN_URL);
		}
	}
	/**
	 * 是否登录
	 * @return  flase已登录,true未登录
	 */
	private function isLogin(){
		if( empty($_SESSION['UIN']) || empty($_SESSION[DOMAIN_ACT_ZONEID_KEY]) ){
			return true;
		}
		
		$this->uin	= $_SESSION['UIN'];
		$this->zoneid = $_SESSION[DOMAIN_ACT_ZONEID_KEY];
		
		$newUser=ChangeUinByZone($this->uin,$this->zoneid);
		$this->uin_new=$newUser['UIN'];
		$this->zoneid_new=$newUser['ZoneID'];
		unset($newUser);
		return false;
	}

//登录处理
	public function save(){		
		load_library('pub:core/FOPassPort', false);
		if(array_key_exists('jumpto', $_GET)){
			$jumpto = urldecode(htmlspecialchars_decode(($_GET['jumpto'])));
		}else{
			$jumpto = DOMAIN_URL;
		}
		if (stripos($jumpto, 'xx5.com') === false){
			die('error');
		}
		$zone=get_dmz_game_zone();

		if(!isset($_GET['gamezone']) || !array_key_exists($_GET['gamezone'],$zone)){
			session_unset();
			dumpErrorCode('游戏分区有误，请重新登录！', 'http://passport.xx5.com/logout.php?gourl='.urlencode(DOMAIN_URL));
		}		
		$_SESSION[DOMAIN_ACT_ZONEID_KEY]		= $_GET['gamezone'];
		if( empty($_SESSION['UIN']) || empty($_SESSION[DOMAIN_ACT_ZONEID_KEY])  ){
			$result=false;
		}else{
			$result=$this->model->getGameUserInfo($_SESSION['UIN'],$_SESSION[DOMAIN_ACT_ZONEID_KEY]);
		}		
		if (!empty($result)){
			$_SESSION['Gender'] = $result['Gender'];
			$_SESSION['RoleName']= $result['RoleName'];
			$_SESSION['Level']=$result['Level'];
			$_SESSION['Money'] = $result['Money'];
			$_SESSION['IP']=get_client_ip_from_ns(true);
			$zones = get_dmz_game_zone();
			$_SESSION['zoneName']=$zones[$_SESSION[DOMAIN_ACT_ZONEID_KEY]];
			$rows=$this->model->getRow('survey',"where uin={$_SESSION['UIN']} AND zone_id={$_SESSION[DOMAIN_ACT_ZONEID_KEY]}");
			if($rows){
				$_SESSION['HasAnswer'] = true;
			}
		}else{
			session_unset();
			//没有游戏角色时  
			dumpErrorCode('亲~您还未创建游戏角色,暂不能参加本活动 ', 'http://passport.xx5.com/logout.php?gourl='.urlencode(DOMAIN_URL));
		}
		header("Location:".$jumpto);
		exit;
	}
	
	//退出处理
	public function logout()
	{
		session_unset();
		session_destroy();
		$gourl = empty($_GET["jumpto"]) ? "" : $_GET["jumpto"];
		$gourl = empty($gourl) ? DOMAIN_URL : $gourl;
		$gourl = urlencode(htmlspecialchars_decode($gourl));
		header("Location: http://passport.xx5.com/logout.php?gourl=" . $gourl);
		flush();
		exit;
	}
	
	function checkAnswer(){
		$zones = get_dmz_game_zone();
		$zone = $zones[$_SESSION[ACT.'ZoneID']];
		$data = array();
		$rows = $this->model->get_list("uin={$_SESSION['LoginUser']} AND zone_id={$_SESSION[ACT.'ZoneID']}");
		if($rows){
			$_SESSION = array();
			session_destroy();		
			$data['success'] = 1;
			$data['msg'] ='您在'.$zone.'已经回答过调查问卷了，非常感谢您的参与！';
			echo json_encode($data);
			exit;
		}
	}
}
?>